package com.itheima.service.impl;

import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.common.entity.PageResult;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    public boolean addCheckItem(CheckItem checkItem) {
        return checkItemMapper.insert(checkItem) > 0;
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemMapper.findAll();
    }

    @Override
    public boolean deleteById(Long id) {
        return checkItemMapper.deleteById(id) > 0;
    }

    @Override
    public boolean updateCheckItem(CheckItem checkItem) {
        return checkItemMapper.update(checkItem) > 0;
    }

    @Override
    public PageResult findPageByCode(Pageable pageable, String code) {
        int pageNum = pageable.getPageNumber();
        int pageSize = pageable.getPageSize();
        int offset = (pageNum - 1) * pageSize;

        List<CheckItem> rows = checkItemMapper.findByCode(code, offset, pageSize);
        long total = checkItemMapper.countByCode(code);

        return new PageResult(total, rows);
    }

    @Override
    public PageResult findPageByName(Pageable pageable, String name) {
        int pageNum = pageable.getPageNumber();
        int pageSize = pageable.getPageSize();
        int offset = (pageNum - 1) * pageSize;

        List<CheckItem> rows = checkItemMapper.findByName(name, offset, pageSize);
        long total = checkItemMapper.countByName(name);

        return new PageResult(total, rows);
    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemMapper.findById(id);
    }
}